//
//  HotelTableViewCell.m
//  MeterMart
//
//  Created by ENERGY on 2018/11/3.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "HotelTableViewCell.h"

@interface HotelTableViewCell ()
@property (nonatomic, strong)UIImageView * iconImg;//图片
@property (nonatomic, strong)UILabel * titleLab;//名
@property (nonatomic, strong)UILabel * describeLab;//有窗、温泉之类
@property (nonatomic, strong)UILabel * notCancelLab;//不可取消
@property (nonatomic, strong)UILabel * moneyLab;//钱数
@property (nonatomic, strong)NSString * prodeceID;
@property (nonatomic, strong)UILabel * roomNumLab;//房间剩余
@property (nonatomic, strong)NSString * affiliationID;//归属上级id
@end

@implementation HotelTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        __weak typeof (self)weakSelf = self;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.iconImg];
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.describeLab];
        [self.contentView addSubview:self.notCancelLab];
        [self.contentView addSubview:self.moneyLab];
        [self.contentView addSubview:self.borderView];
        [self.borderView addSubview:self.orderLab];
        [self.borderView addSubview:self.onlinePayLab];
        [self.contentView addSubview:self.roomNumLab];
        [self.iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(18/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(24/WIDTH_6_SCALE);
            make.width.mas_offset(105/WIDTH_6_SCALE);
            make.height.mas_offset(85/WIDTH_6_SCALE);
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.iconImg.mas_right).mas_offset(13/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.iconImg);
        }];
        [self.describeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.titleLab);
            make.top.equalTo(weakSelf.titleLab.mas_bottom).mas_offset(8/WIDTH_6_SCALE);
            make.right.equalTo(weakSelf.contentView.mas_right).mas_offset(-75/WIDTH_6_SCALE);
        }];
        [self.notCancelLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.titleLab);
            make.top.equalTo(weakSelf.describeLab.mas_bottom).mas_offset(13/WIDTH_6_SCALE);
        }];
        [self.moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.titleLab);
            make.top.equalTo(weakSelf.notCancelLab.mas_bottom).mas_offset(8/WIDTH_6_SCALE);
        }];
        UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:self.orderLab.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(8/WIDTH_6_SCALE,8/WIDTH_6_SCALE)];
        //创建 layer
        CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
        maskLayer1.frame = self.orderLab.bounds;
        //赋值
        maskLayer1.path = maskPath1.CGPath;
        self.orderLab.layer.mask = maskLayer1;

        UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:self.onlinePayLab.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(8/WIDTH_6_SCALE,8/WIDTH_6_SCALE)];
        //创建 layer
        CAShapeLayer *maskLayer2 = [[CAShapeLayer alloc] init];
        maskLayer2.frame = self.onlinePayLab.bounds;
        //赋值
        maskLayer2.path = maskPath2.CGPath;
        self.onlinePayLab.layer.mask = maskLayer2;
    }return self;
}
- (UIImageView *)iconImg{
    if (!_iconImg) {
        _iconImg = [[UIImageView alloc] init];
        _iconImg.backgroundColor = [UIColor redColor];
        _iconImg.image = [UIImage imageNamed:@"YimiPlaceHolder"];
    }return _iconImg;
}
- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = DEF_FontSize_10;
        _titleLab.textColor = [UIColor blackColor];
        _titleLab.textAlignment = NSTextAlignmentLeft;
        _titleLab.text = @"--";
    }return _titleLab;
}
- (UILabel *)describeLab{
    if (!_describeLab) {
        _describeLab = [[UILabel alloc] init];
        _describeLab.font = DEF_FontSize_8;
        _describeLab.textColor = getColor(textGrayColor);
        _describeLab.textAlignment = NSTextAlignmentLeft;
        _describeLab.text = @"--";
        _describeLab.lineBreakMode = 0;
        _describeLab.numberOfLines = 0;
    }return _describeLab;
}
- (UILabel *)notCancelLab{
    if (!_notCancelLab) {
        _notCancelLab = [[UILabel alloc] init];
        _notCancelLab.font = DEF_FontSize_8;
        _notCancelLab.textColor = getColor(textGrayColor);
        _notCancelLab.textAlignment = NSTextAlignmentLeft;
        _notCancelLab.text = @"不可取消";
    }return _notCancelLab;
}
- (UILabel *)moneyLab{
    if (!_moneyLab) {
        _moneyLab = [[UILabel alloc] init];
        _moneyLab.font = DEF_FontSize_16;
        _moneyLab.textColor = getColor(mainRedColor);
        _moneyLab.textAlignment = NSTextAlignmentLeft;
        _moneyLab.text = @"￥--";
    }return _moneyLab;
}
- (UIView *)borderView{
    if (!_borderView) {
        _borderView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 67/WIDTH_6_SCALE, 40.5/WIDTH_6_SCALE, 49/WIDTH_6_SCALE, 40/WIDTH_6_SCALE)];
        _borderView.layer.borderColor = getColor(mainRedColor).CGColor;
        _borderView.layer.borderWidth = 1;
        _borderView.backgroundColor = [UIColor whiteColor];
        _borderView.layer.cornerRadius = 8;
        _borderView.layer.masksToBounds = YES;
    }return _borderView;
}
- (UILabel *)orderLab{
    if (!_orderLab) {
        _orderLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.borderView.width, self.borderView.height/2 + 3/WIDTH_6_SCALE)];
        _orderLab.font = DEF_FontSize_10;
        _orderLab.textColor = [UIColor whiteColor];
        _orderLab.textAlignment = NSTextAlignmentCenter;
        _orderLab.backgroundColor = getColor(mainRedColor);
        _orderLab.text = @"预定";
    }return _orderLab;
}
- (UILabel *)onlinePayLab{
    if (!_onlinePayLab) {
        _onlinePayLab = [[UILabel alloc] initWithFrame:CGRectMake(0, self.orderLab.bottom, self.orderLab.width, self.borderView.height - self.orderLab.height)];
        _onlinePayLab.font = DEF_FontSize_8;
        _onlinePayLab.textColor = getColor(mainRedColor);
        _onlinePayLab.textAlignment = NSTextAlignmentCenter;
        _onlinePayLab.backgroundColor = [UIColor whiteColor];
        _onlinePayLab.text = @"在线付";
    }return _onlinePayLab;
}
- (UILabel *)roomNumLab{
    if (!_roomNumLab) {
        _roomNumLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100/WIDTH_6_SCALE - 27/WIDTH_6_SCALE, 94/WIDTH_6_SCALE, 100/WIDTH_6_SCALE, 15/WIDTH_6_SCALE)];
        _roomNumLab.font = DEF_FontSize_8;
        _roomNumLab.textColor = getColor(textGrayColor);
        _roomNumLab.textAlignment = NSTextAlignmentRight;
        _roomNumLab.backgroundColor = [UIColor whiteColor];
    }return _roomNumLab;
}
- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.prodeceID = model[@"id"];
    [self.iconImg sd_setImageWithURL:[NSURL URLWithString:model[@"goodsimg"]] placeholderImage:[UIImage imageNamed:@"YimiPlaceHolder"]];
    self.titleLab.text = model[@"goodsname"];
    self.describeLab.text = model[@"goodsintro"];
    self.moneyLab.text = [NSString stringWithFormat:@"￥%@", model[@"groupprice"]];
    [self changeTheSpecifiedString:self.moneyLab.text andSpecifiedCharacters:@[@"￥"] andFont:10 andTheLabel:self.moneyLab];
    if ([model[@"ishotel"] isEqualToString:@"1"]) {
        self.roomNumLab.text = [NSString stringWithFormat:@"剩余%@间房", model[@"roomcount"]];
        self.roomNumLab.hidden = NO;
    }else{
        self.roomNumLab.hidden = YES;
    }
    self.affiliationID = model[@"typeid_goods"];
}
- (void)changeTheSpecifiedString:(NSString *)specifiedString andSpecifiedCharacters:(NSArray *)characters andFont:(NSInteger)fontSize andTheLabel:(UILabel *)specifiedLab{
    NSString * titleString = specifiedString;
    NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString:titleString];
    for (NSString * str in characters) {
        NSRange range = [titleString rangeOfString:str];
        [attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize] range:range];
    }
    [specifiedLab setAttributedText:attribute];
}
@end
